/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.proto.itemserverprotoVo.spriteRequest;
import com.dc.gameserver.proto.itemserverprotoVo.spriteResponse;
import com.dc.gameserver.proto.itemserverprotoVo.spriteinformation;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TSprite;
import com.dc.gameserver.serverCore.model.gamemodel.ItemCell;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:14-1-13 </br>
 *         Time:下午4:06 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment：
 */
@Service
public class spriteInfomationController extends AbstractController {

    @Resource
    private ModelCacheService modelCacheService;
    @Resource
    private PlayerService playerService;

    @Override
    public void PostConstruct() {
        int ID= spriteRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=spriteRequest.getDefaultInstance();
    }


    /**
     message spriteResponse{
     enum msgID {
     ID = 2022;
     }
     required uint32 spriteStore=1;//当前功法中存储的灵气
     repeated spriteinformation inforList=2;//灵泉对象列表
     }
     * 1.当前功法中存储的灵气值
     * 2.装备的灵泉中灵气剩余值
     * @param messageLite     数据载体
     * @param player active object
     * @throws Exception
     */
    @Override
    @SuppressWarnings("unchecked")
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        int roleID=player.getID();
        String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
        TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);
        spriteResponse.Builder builder=spriteResponse.newBuilder();


        List<TSprite> spriteList=(List<TSprite>) modelCacheService.findListByPrefixID(ModelCacheService.SPRITE_PREFIX(roleID));
        if (checkSprite(player,tRole)){
            spriteCompute(player,tRole,System.currentTimeMillis(),modelCacheService,playerService,spriteList,false);
            playerService.updateRole(tRole);
            modelCacheService.addObjectPrefixID(role_prefix,tRole);
        }
        builder.setSpriteStore(tRole.getStorelingqi());
        if(spriteList!=null){
            FastTable<ItemCell> spriteCellList=player.getSpriteItem();
            for (ItemCell cell:spriteCellList){
                int itemID=cell.getItemID();
                for (TSprite sprite:spriteList){
                    if (sprite.getSpiriteid()==itemID){
                        spriteinformation.Builder _builder=spriteinformation.newBuilder();
                        _builder.setSpriteLocation(cell.getItemLocation());
                        _builder.setSpriteLast(sprite.getSpiritecurrent());  //灵泉当前剩余值
                        builder.addInforList(_builder.build());
                        break;
                    }
                }
            }
            spriteCellList.clear();
            spriteCellList=null;
        }
        player.DirectSend(wrappedBufferShort(spriteResponse.msgID.ID_VALUE,builder.build()));
    }
}